﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZealousTTS.Common.Extensions;
using ZealousTTS.Domain;
using ZealousTTS.Web.ViewModels;
using ZealousTTS.Web.Utility;
using AutoMapper;

namespace ZealousTTS.Web.Areas.Private.Controllers
{
    public class PrioritiyController : BaseController
    {
		private ZealousTTSEntities _Context;
        
		#region Constructors
		public PrioritiyController()
		{
			_Context = new ZealousTTSEntities();
		}

		public PrioritiyController(UserViewModel currentUser)
			: base(currentUser)
        {
			_Context = new ZealousTTSEntities();
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index()
        {
            return View();
        }
		/// <summary>
        /// Gets a Prioritiy from Database to edit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
		public ActionResult Edit(int id = 0)
        {
            Prioritiy entity = id == 0 ? new Prioritiy() : _Context.Prioritiys.FirstOrDefault(P => P.ID == id);            
            PrioritiyViewModel viewModel = Mapper.Map<Prioritiy, PrioritiyViewModel>(entity);             
            return View(viewModel);
        }

		/// <summary>
        /// Saves Prioritiy Information into the Database
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
		[HttpPost]
		public ActionResult Edit(PrioritiyViewModel viewModel)
        {
			if (!ModelState.IsValid)
                return View(viewModel);

			Prioritiy entity = null; 
			if (viewModel.ID == 0)
            {
                entity = new Prioritiy();
                _Context.AddToPrioritiys(entity);
            }
            else
                entity = _Context.Prioritiys.FirstOrDefault(P => P.ID == viewModel.ID);

            entity = Mapper.Map<PrioritiyViewModel, Prioritiy>(viewModel, entity);
            if (viewModel.ID == 0)
            {
                entity.Name = "";
            }
            _Context.SaveChanges();
            return RedirectToAction("Index", "Home"); 
        }		
		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}
